package com.example.day4;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    FragmentManager fm;
    Button suyue,yao;
    Fragment f1;
    MyFragment2 f2;
    boolean isShow=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        suyue = findViewById(R.id.su);
        yao=findViewById(R.id.yao);
        f1= new MyFragment();
        f2 = new MyFragment2();
        fm = getSupportFragmentManager();//获得Fragment管理类
        FragmentTransaction transaction = fm.beginTransaction();//进行本次管理（每次管理需要重新获得）

        transaction.add(R.id.frame_home2,f1,"f2");
        transaction.add(R.id.frame_home2,f2,"f1");
        //其他方法
        //transaction.replace(父容器,frragment对象)；
        //transaction.remove(frragment对象)；
        //transaction.hide(frragment对象)；
        //transaction.show(frragment对象)；
        transaction.commit();
        yao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                f2.wang.setText("刘X");
            }
        });
        suyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//
                FragmentTransaction transaction2 = fm.beginTransaction();//进行本次管理（每次管理需要重新获得）
//                transaction2.replace(R.id.frame_home,f1,"f2");
                if(isShow){
                    transaction2.hide(f2);
//                    transaction2.replace(R.id.frame_home,f1,"f2");

                }else{
                    transaction2.show(f2);
//                    transaction2.replace(R.id.frame_home,f2,"f2");
                }
               isShow=!isShow;
                transaction2.commit();
            }
        });

    }
}
